/**
 * 2025/01/01 - 2025/12/31
 */
package com.snail.server.database.engine.ddl.field.manager;

import com.snail.server.database.engine.ddl.field.dto.FieldAddDTO;
import com.snail.server.database.engine.ddl.field.enums.FieldTypeEnum;
import com.snail.server.database.engine.ddl.model.field.FieldMo;
import com.snail.starter.db.enums.YesOrNoEnum;
import com.snail.starter.utils.lambda.GenericEntityBuilder;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zgadmin
 * @date 2025/8/29 14:33
 */
@Component
public class FieldManager {

    public void create(Long dataSourceId, Long tableId, FieldAddDTO fieldCreate) {
    }

    public void alter(Long dataSourceId, Long tableId, Long fileId) {
    }

    public void queryById(Long dataSourceId, Long tableId, Long fileId) {
    }

    public void delete(Long dataSourceId, Long tableId, Long fileId) {
    }

    public static List<FieldMo> toFieldMo(List<FieldAddDTO> fieldAddList) {
        if (null == fieldAddList || fieldAddList.isEmpty()) {
            return new ArrayList<>();
        }
        return fieldAddList.stream().map(FieldManager::toFieldMo).distinct().collect(Collectors.toList());
    }

    public static FieldMo toFieldMo(FieldAddDTO fieldAdd) {
        return GenericEntityBuilder.of(FieldMo::new)
                .withSupplier(FieldMo::setName, fieldAdd::getName)
                .withSupplier(FieldMo::setType, () -> FieldTypeEnum.getEnum(fieldAdd.getType()))
                .withSupplier(FieldMo::setPrimaryKey, () -> fieldAdd.isPrimaryKey() ? YesOrNoEnum.YES : YesOrNoEnum.NO)
                .withSupplier(FieldMo::setAutoIncrement, () -> fieldAdd.isAutoIncrement() ? YesOrNoEnum.YES : YesOrNoEnum.NO)
                .withSupplier(FieldMo::setNotNull, () -> fieldAdd.isNotNull() ? YesOrNoEnum.YES : YesOrNoEnum.NO)
                .withSupplier(FieldMo::setIDigits, fieldAdd::getIDigits)
                .withSupplier(FieldMo::setDDigits, fieldAdd::getDDigits)
                .withSupplier(FieldMo::setDefaultValue, fieldAdd::getDefaultValue)
                .withSupplier(FieldMo::setDescription, fieldAdd::getDescription)
                .build();
    }
}
